<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur ()){
    include_once (DS . "backoffice/include/header.php");
    include_once( DS . "include/zones.php");

    if (isset($_POST['submit']))
    {
        $entreprise = new Entreprise();
        // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
        $form = true;
        $errMSG = array();


        /* --------------------- INFORMATIONS ----------------------- */
        if (isset($_POST['lastname']) && !empty($_POST['lastname']))
        {
            $tmp = $entreprise->setNomEntreprise($_POST['lastname']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['description']) && !empty($_POST['description']))
        {
            $tmp = $entreprise->setDescriptionEntreprise($_POST['description']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['description'] = $tmp['errmsg'];
                $form = false;
            }
        }
        /* ------------------ FIN INFORMATIONS --------------------- */

        /* ----------------------- CONTACT ------------------------- */
        if (isset($_POST['email']) && !empty($_POST['email']))
        {
            $tmp = $entreprise->setMailEntreprise($_POST['email']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['tel']) && !empty($_POST['tel']))
        {
            $tmp = $entreprise->setTelephoneEntreprise($_POST['tel']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['tel'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['site']) && !empty($_POST['site']))
        {
            $tmp = $entreprise->setSiteEntreprise($_POST['site']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['site'] = $tmp['errmsg'];
                $form = false;
            }
        }
        /* -------------------- FIN CONTACT ----------------------- */

        /* ----------------------- ADRESSE ------------------------ */
        if (isset($_POST['adresse']) && !empty($_POST['adresse']))
        {
            $tmp = $entreprise->setAdresse1Entreprise($_POST['adresse']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['add1'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['adresse2']) && !empty($_POST['adresse2']))
        {
            $tmp = $entreprise->setAdresse2Entreprise($_POST['adresse2']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['add2'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['city']) && !empty($_POST['city']))
        {
            $tmp = $entreprise->setVilleEntreprise($_POST['city']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['ville'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['cp']) && !empty($_POST['cp']))
        {
            $tmp = $entreprise->setCPEntreprise($_POST['cp']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['cp'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['zone']) && !empty($_POST['zone']))
        {
            $tmp = $entreprise->setIdZone($_POST['zone']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['zone'] = $tmp['errmsg'];
                $form = false;
            }
        }
        /* -------------------- FIN ADRESSE ----------------------- */

        if ($form === true)
        {
            $entreprise->insertEntreprise();
            redirectTo(URLBACK."_userentreprise_view&uid=".$entreprise->getIdEntreprise());
        }
    }
    ?>
    <?php
    include_once( DS . "backoffice/include/toppanel_utilisateurs.php");
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box"><
                <h3 id="adduser">Ajout d'une entreprise</h3>
                <form id="form" method="post">
                    <fieldset id="personal">
                        <legend>Informations</legend>
                        <label for="lastname">Nom : </label>
                        <input name="lastname" id="lastname" type="text" tabindex="1" />
                        <br />
                        <label for="description">Description : </label>
                        <textarea name="description" id="description" type="text" tabindex="1"></textarea>
                        <br />
                    </fieldset>
                    <fieldset id="personal">
                        <legend>Contact</legend>
                        <label for="tel">Telephone : </label>
                        <input name="tel" id="email" type="text"tabindex="2" />
                        <br />
                        <label for="email">Email : </label>
                        <input name="email" id="email" type="text"tabindex="2" />
                        <br />
                        <label for="site">Site : </label>
                        <input name="site" id="site" type="text"tabindex="2" />
                    </fieldset>
                    <fieldset id="address">
                        <legend>Adresse</legend>
                        <label for="adresse">Adresse : </label>
                        <input name="adresse" id="adresse" type="text" tabindex="1" />
                        <br />
                        <label for="adresse2">Adresse + : </label>
                        <input name="adresse2" id="adresse2" type="text" tabindex="1" />
                        <br />
                        <label for="cp">Code Postal : </label>
                        <input name="cp" id="cp" type="text" tabindex="1" />
                        <br />
                        <label for="city">Ville : </label>
                        <input name="city" id="city" type="text" tabindex="2" />
                        <br />
                        <label for="zones">Situation : </label>

                        <b>France, Alpes-Maritimes/Monaco : </b>
                        <br/>
                        <div style="margin-left:140px;" id="situationGeo">
                        <?php
                                foreach ($zones as $idZone => $zone)
                                {
                                    $str = "<b>" . $zone['nom'] . "</b><br/>(<i>";
                                    $nbVille = 1;
                                    foreach ($zone['ville'] as $idVille => $ville)
                                    {
                                        if ($nbVille == count($zone['ville']))
                                        {
                                            $str .= $ville;
                                        }
                                        else
                                        {
                                            $str .= $ville . ", ";
                                        }
                                        $nbVille += 1;
                                    }
                                    $str .= "</i>)";
                                ?>
                                    <input <?php if ($idZone == 1)
                                    {
                                        echo 'checked';
                                    } ?> type="radio" name="zone" value="<?php echo $idZone; ?>"/> <?php echo $str; ?><br/>
    <?php
                                }
    ?>
                        </div>
                        <div style="margin-left:120px;" id="situation">
                        <input type="radio" name="zone" value="12"/> <b>Autre d&eacute;partement</b>
                        <br/>
                        <input type="radio" name="zone" value="0"/> <b>Etranger</b>
                        </div>
                    </fieldset>
                    <br /><span class="error"><?php
                                if (isset($errMSG['types']))
                                {
                                    echo $errMSG['types'];
                                }
    ?></span>
                            <div align="center">
                                <input id="button1" type="submit" name="submit" value="Ajouter" />
                                <input id="button2" type="reset" />
                            </div>
                        </form>
                    </div>
                </div>
        <?php
        include_once (DS . "backoffice/include/sidebar.php");
        include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo();
}
?>